/**
 * Created by WYH on 17/4/25.
 */
angular.module('rentRenew', []).component(
  'rentRenew',
  {
    templateUrl: 'component/rent/rentRenew/rent_renew.template.html',
    controller: ['$scope', 'RentProtocolModel', 'Api','Alert', function ($scope, RentProtocolModel, Api,Alert) {
      
      $scope.renewRentProtocol = clone($scope.$parent.rentProtocol)|| new RentProtocolModel();
      $scope.rentProtocol = new RentProtocolModel();
      $scope.limitTime = undefined;
      
      $scope.onTimeSetStart = onTimeSetStart;
      $scope.onTimeSetEnd = onTimeSetEnd;
      $scope.submitRenewd = submitRenewd;
      
      $scope.$parent.$watch('rentProtocol', function (rentProtocol) {
        $scope.renewRentProtocol = clone(rentProtocol);
      });
      $scope.checkCountPayCycle=checkCountPayCycle;
      $scope.checkCountMonthlyRent=checkCountMonthlyRent;
      
      function checkCountPayCycle(value) {
        if (value<0){
          Alert.alertBox('输入值不可为负数')
          $scope.rentProtocol.payCycle = 0
        }
      }
      function checkCountMonthlyRent(value) {
        var pattern =/^[0-9]+([.]\d{1,2})?$/;
        if(value && !pattern.test(value)){
          Alert.alertBox('最多保留两位小数');
          $scope.rentProtocol.monthlyRent=Number(value.toFixed(2));
        }
        if (value<0){
          Alert.alertBox('输入值不可为负数')
          $scope.rentProtocol.monthlyRent = 0
        }
      }
      
      function onTimeSetStart(newDate, oldDate) {
        $scope.rentProtocol.beginTime = newDate.getTime();
        $scope.showStartTime = false;
      }
      
      function onTimeSetEnd(newDate, oldDate) {
        $scope.rentProtocol.endTime = newDate.getTime();
        $scope.showEndTime = false;
      }
      
      function submitRenewd(rentProtocol) {
        rentProtocol.type = "OFFICE";
        rentProtocol.ownerId = $scope.renewRentProtocol.companyId;
        rentProtocol.roomNum = $scope.renewRentProtocol.roomNum;
        Api.createRentProtocol(rentProtocol, function (data) {
          Alert.alertBox('续租成功',function () {
            return window.location.reload();
          });
        })
      }
      
      function clone(data) {
        var new_data = {};
        for (var key in data) {
          new_data[key] = data[key]
        }
        return new_data
      }
      
      
      Date.prototype.setIntYear = function () {
        this.setUTCMonth(0);
        this.setUTCDate(1);
        this.setUTCHours(0, 0, 0, 0)
      };
      Date.prototype.setIntMonth = function () {
        this.setUTCDate(1);
        this.setUTCHours(0, 0, 0, 0)
      };
      Date.prototype.setIntDay = function () {
        this.setUTCHours(this.getUTCHours(), 0, 0, 0)
      };
      
      $scope.beginDateBeforeRender = function ($view, $dates) {
          console.log($scope.$parent.rentProtocol)
        if($scope.limitTime === undefined){
          Api.getRentEndTime($scope.renewRentProtocol.companyId,function(limitTime){
            $scope.limitTime = limitTime ? limitTime + 24*60*60*1000 : limitTime;
            if(limitTime){
              $scope.rentProtocol.beginTime  =  $scope.limitTime
            }
            setBeginDateBeforeRender($view, $dates)
          })
        }else{
          setBeginDateBeforeRender($view, $dates)
        }
      };
      
      
      $scope.endDateBeforeRender = function ($view, $dates) {
        if($scope.limitTime === undefined){
          Api.getRentEndTime($scope.renewRentProtocol.companyId,function(limitTime){
            $scope.limitTime = limitTime ? limitTime + 24*60*60*1000 : limitTime
            setEndDateBeforeRender($view, $dates)
          })
        }else{
          setEndDateBeforeRender($view, $dates)
        }
      };
      
      
      function setEndDateBeforeRender($view, $dates){
        var limitDate
        var todaySinceMidnight = new Date($scope.rentProtocol.beginTime);
        if($scope.limitTime){
          limitDate = new Date($scope.limitTime);
        }else{
          limitDate = new Date();
          limitDate.setUTCMonth(limitDate.getMonth() - 1);
          limitDate.setIntMonth();
        }
        
        
        if ($view == 'year') {
          todaySinceMidnight.setIntYear();
          limitDate.setIntYear()
        }
        if ($view == 'month') {
          todaySinceMidnight.setIntMonth();
          limitDate.setIntMonth()
        }
        if ($view == 'day') {
          todaySinceMidnight.setIntDay();
          limitDate.setIntDay()
          todaySinceMidnight.setHours(todaySinceMidnight.getDate()+8)
        }
        
        $dates.filter(function (date) {
          if ($view == 'day') {
            return date.utcDateValue <= todaySinceMidnight.getTime() || date.utcDateValue <= limitDate.getTime();
          }
          return date.utcDateValue < todaySinceMidnight.getTime() || date.utcDateValue < limitDate.getTime();
          
          
        }).forEach(function (date) {
          
          date.selectable = false;
        });
        $dates.filter(function (date) {
          if ($view == 'day') {
            return date.utcDateValue > todaySinceMidnight.getTime() && date.utcDateValue > limitDate.getTime();
          } else {
            return date.utcDateValue >= todaySinceMidnight.getTime() && date.utcDateValue >= limitDate.getTime();
          }
          
        }).forEach(function (date) {
          date.selectable = true;
        });
      }
      
      function setBeginDateBeforeRender($view, $dates){
        var limitDate
        if($scope.limitTime){
          limitDate = new Date($scope.limitTime);
        }else{
          limitDate = new Date();
          limitDate.setUTCMonth(limitDate.getMonth() - 1);
          limitDate.setIntMonth();
        }
        
        var todaySinceMidnight = new Date($scope.rentProtocol.endTime);
        
        
        if ($view == 'year') {
          todaySinceMidnight.setIntYear()
          limitDate.setIntYear()
        }
        if ($view == 'month') {
          todaySinceMidnight.setIntMonth()
          limitDate.setIntMonth()
        }
        if ($view == 'day') {
          todaySinceMidnight.setIntDay()
          limitDate.setIntDay()
        }
        $dates.filter(function (date) {
          
          if ($view == 'day') {
            return date.utcDateValue >= todaySinceMidnight.getTime() || date.utcDateValue < limitDate.getTime()
          }
          return (date.utcDateValue < limitDate.getTime() || date.utcDateValue > todaySinceMidnight.getTime());
          
        }).forEach(function (date) {
          // console.log(date.utcDateValue)
          date.selectable = false;
        });
        $dates.filter(function (date) {
          if ($view == 'day') {
            return date.utcDateValue < todaySinceMidnight.getTime() && date.utcDateValue >= limitDate.getTime()
          }
          return date.utcDateValue <= todaySinceMidnight.getTime() && date.utcDateValue >= limitDate.getTime();
        }).forEach(function (date) {
          date.selectable = true;
        });
      }
    }]
  }
);